//
//  PhoneticReadingManager.m
//  ebook
//
//  Created by mt on 16/10/26.
//  Copyright © 2016年 Moton. All rights reserved.
//

#import "PhoneticReadingManager.h"
#import <AVFoundation/AVFoundation.h>

@interface PhoneticReadingManager ()<AVSpeechSynthesizerDelegate>

@property (nonatomic, strong) AVSpeechSynthesizer *synthesizer;

@end

@implementation PhoneticReadingManager

+ (instancetype)shareManager {
    
    static PhoneticReadingManager *phoneticReadingManager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        phoneticReadingManager = [[self alloc] init];
    });
    return phoneticReadingManager;
}

- (AVSpeechSynthesizer *)synthesizer {
    
    if (!_synthesizer) {
        _synthesizer = [[AVSpeechSynthesizer alloc] init];
        _synthesizer.delegate = self;
    }
    return _synthesizer;
}

- (void)startPhoneticReadingText:(NSString *)content {
    
    AVSpeechUtterance * speech = [[AVSpeechUtterance alloc] initWithString:content];
    AVSpeechSynthesisVoice *voiceType = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-TW"];//[AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
    speech.voice = voiceType;//语种配置
    /*
     *      1         2       3       4       5
     *     0.4       0.45     0.5     0.55     0.6
     *语速取值范围0～1， 默认0.5
     */
    speech.rate = ((iOS9?0.35:0.05)+(_rate-1)*0.05);//语速
    speech.pitchMultiplier = 0.9f;//音高
    speech.postUtteranceDelay = 0;//读完一段后的停顿时间
//    speech.preUtteranceDelay = 1;
    
    [self.synthesizer speakUtterance:speech];//生成语音
}

- (void)stopPhoneticReading {
    [self.synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
    self.synthesizer.delegate = nil;
    self.synthesizer = nil;
}

- (void)pausePhoneticReading {
    [self.synthesizer pauseSpeakingAtBoundary:AVSpeechBoundaryImmediate];
}

- (void)continuePhoneticReading {
    [self.synthesizer continueSpeaking];
}

#pragma mark - AVSpeechSynthesizerDelegate

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didStartSpeechUtterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"didStartSpeechUtterance:%@", utterance.speechString);
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"didFinishSpeechUtterance:%@", utterance.speechString);
    if ([self.delegate respondsToSelector:@selector(phoneticreadingManager:didFinishSpeechUtterance:)]) {
        [self.delegate phoneticreadingManager:self didFinishSpeechUtterance:utterance];
    }
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"didPauseSpeechUtterance:%@", utterance.speechString);
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"didContinueSpeechUtterance:%@", utterance.speechString);
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer didCancelSpeechUtterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"didCancelSpeechUtterance:%@", utterance.speechString);
}

- (void)speechSynthesizer:(AVSpeechSynthesizer *)synthesizer willSpeakRangeOfSpeechString:(NSRange)characterRange utterance:(AVSpeechUtterance *)utterance {
    
//    MTLog(@"willSpeakRangeOfSpeechString:%@", [utterance.speechString substringWithRange:characterRange]);
}



@end
